package org.sgx.gwteditors.client.impl1.complex;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.sgx.gwteditors.client.editor.Editor;
import org.sgx.gwteditors.client.editor.EditorNotFound;
import org.sgx.gwteditors.client.editor.Types;
import org.sgx.gwteditors.client.editor.error.EditorError;
import org.sgx.gwteditors.client.editor.error.EditorErrorManager;
import org.sgx.gwteditors.client.editor.event.EditorEventManager;
import org.sgx.gwteditors.client.editor.event.ValueChangeListener;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.EditorManager1;
//import org.sgx.gwteditors.client.impl1.EditorFactoryImpl;
//import org.sgx.gwteditors.client.impl1.bean.SimpleBeanEditor1;

import com.google.gwt.core.client.ScriptInjector;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;

/**
 * matrix 2d editor, all members must be same type. This is a very simple editor
 * that will show editors for given objects in a row. 
 * @author sg
 */
public class Array1DEditor<T> extends FlexTable implements Editor<T[]> {
	
	static Logger logger = Logger.getLogger(Array1DEditor.class+"");
	Map<Integer, Editor<Object>> eds = null;
	private int imax;
	private int jmax;
//	private Object[] model;
	private String type;
	private boolean vertical;
	
	public Array1DEditor(String type, boolean vertical) {
		super();
		eds = new HashMap<Integer, Editor<Object>> ();
		this.type=type;
		this.vertical=vertical;
	}

	@Override
	public void addValueChangeListener(ValueChangeListener<T[]> l) {
		EditorEventManager.<T[]>getInstance().addValueChangeListener(this, l);
	}

	@Override
	public void removeValueChangeListener(ValueChangeListener<T[]> l) {
		EditorEventManager.<T[]>getInstance().addValueChangeListener(this, l);
	}

	@Override
	public List<EditorError<T[]>> getErrors() {
		return EditorErrorManager.<T[]>getInstance().getErrors(this);
	}
	@Override
	public T[] flush() {
		T[] vals = (T[]) new Object[imax][jmax];
		for(int i=0; i<this.imax; i++) {
			Editor<Object> ed = eds.get(i);
			if(ed==null)
				continue;
			T val = (T) ed.flush();
			vals[i]=val;
		}			
		return vals;//no validation / errors
	}

	@Override
	public void load(T[] model) {
		
		if(model==null)
			return;
		//clear the table
		clear();
//		this.model=model;
		this.imax=model.length;
		for(int i=0; i<model.length; i++) {
			Object o = model[i];
			if(o==null)
				continue;
			Editor<Object> ed=null;
			try {
				ed = Types.getInstance().newEditor(type);//EditorFactoryImpl.<Object>getInstance().getEditorForObject(o);
				
				ed.load(o);
				ed.getWidget().getElement().getStyle().setProperty("paddingBottom", "20px");
				ed.getWidget().getElement().getStyle().setProperty("borderBottom", "1px solid #ededed"); 
				if(vertical)
					setWidget(i, 0, ed.getWidget());
				else
					setWidget(0, i, ed.getWidget());
				
				eds.put(i, ed);
			} catch (EditorNotFound e) {
				logger.log(Level.WARNING, " exception building editor for field "+i+
						", class: "+o.getClass()+" : "+o+
						". Cause: "+e.getCause()+" - "+e.getMessage());
				e.printStackTrace();
			}
//			this.jmax=model[i].length;
//			for(int j=0; j<model[i].length; j++) {
//				Object o = model[i][j];
//				if(o==null)
//					continue;
//				Editor<Object> ed=null;
//				try {
//					ed = EditorFactoryImpl.<Object>getInstance().getEditorForObject(o);
//					ed.load(o);
//					setWidget(i, j, ed.getWidget());
//					eds.put(i+","+j, ed);
//				} catch (EditorNotFound e) {
//					logger.log(Level.WARNING, " exception building editor for field "+i+
//							", "+j+", class: "+model[i][j].getClass()+" : "+o+
//							". Cause: "+e.getCause()+" - "+e.getMessage());
//					e.printStackTrace();
//				}
//			}
		}		
	}

	@Override
	public Widget getWidget() {
		return this;
	}

	@Override
	public boolean isSimple() {		
		return false;
	}

	@Override
	public String getEditorId() {
		return EditorManager1.getInstance().getEditorName(this);		
	}

	@Override
	public String[] getEditorTypesSupported() {
		return null; //String[]{EditorFramework1.TYPE_}; 
	}

}
